#include "dialogmkdir.h"
#include "ui_dialogmkdir.h"
#include <QRegularExpression>
#include <QMessageBox>

DialogMkdir::DialogMkdir(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::DialogMkdir)
{
    ui->setupUi(this);
    connect(ui->pushButton_ok,&QPushButton::clicked,this,&DialogMkdir::onConfirm);
}

DialogMkdir::~DialogMkdir()
{
    delete ui;
}

bool DialogMkdir::isLegalFolderName(const QString &fileName){
    QRegularExpression regex(R"(^[a-zA-Z0-9_\-.][a-zA-Z0-9_\-. ]*$)");
    return regex.match(fileName).hasMatch();
}

void DialogMkdir::onConfirm(){
    QString foldername=ui->lineEdit->text();
    if(isLegalFolderName(foldername)){
        emit confirmed(foldername);
        ui->lineEdit->clear();
        close();
    }else{
        QMessageBox::critical(this, tr("Warning"),  tr("The folder name is illegal!"),
                              QMessageBox::Ok | QMessageBox::Cancel,  QMessageBox::Ok);
        ui->lineEdit->selectAll();
    }
}
